<template>
    <!-- 标题区域 -->
    <van-nav-bar title="欢迎登录" left-text="返回" left-arrow @click-left="() => { $router.push('/') }" fixed />
    <div style="height: 45px;"></div>
    <!-- 注册页面排版  表单-->
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]" />
            <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]" />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
                登录
            </van-button>
        </div>
    </van-form>
</template>

<script setup>
// 引入 ref
import { ref } from 'vue'
// 引入 注册接口
import { loginApi } from '@/api/api'
// 引入 路由跳转
import { useRouter } from 'vue-router'
const router = useRouter();
// 引入轻提示
import { showSuccessToast, showFailToast } from 'vant';
// 自定义 双向绑定数据
let username = ref('')
let password = ref('')
// 点击按钮 onSubmit  事件 触发绑定
const onSubmit = () => {
    // 发送注册接口
    loginApi(username.value,password.value).then(res => {
        console.log('登录数据',res);
        if(res.data.code == 0){
            showSuccessToast('登录成功');
            // 本地存储 用户名和 token
            localStorage.setItem('username', res.data.data.username)
            localStorage.setItem('token', res.data.data.token)
            router.push('/')
        }else{
            showFailToast('登录失败');
        }
    })
}

</script>

<style lang="scss" scoped></style>